package com.api.shopapi.mapper;

import com.api.shopapi.entity.DeviceTogWatch;
import com.api.shopapi.entity.dto.DeviceTogWatchDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface DeviceTogWatchMapper {

    @Insert("insert into device_togwatch " +
            "(" +
            "DeviceTogwatch, " +
            "UserCode, " +
            "Imei, " +
            "BindTime " +
            ") " +
            "VALUES " +
            "(" +
            "#{deviceTogWatch.DeviceTogwatch} ," +
            "#{deviceTogWatch.UserCode} ," +
            "#{deviceTogWatch.Imei}, " +
            "#{deviceTogWatch.BindTime} " +
            ")")
    int insert(@Param("deviceTogWatch") DeviceTogWatch deviceTogWatch);


    @Update("update device_togwatch set " +
            "UserCode=#{deviceTogWatch.UserCode} ," +
            "Imei=#{deviceTogWatch.Imei}, " +
            "BindTime=#{deviceTogWatch.BindTime} " +
            "where " +
            "DeviceTogwatch=#{deviceTogWatch.DeviceTogwatch}   ")
    int update(@Param("deviceTogWatch") DeviceTogWatch deviceTogWatch);


    @Select("select * from device_togwatch where DeviceTogwatch=#{deviceTogWatchCode} limit 1 ")
    DeviceTogWatch getByDeviceTogWatchCode(@Param("deviceTogWatchCode") String deviceTogWatchCode);


    @Select("select  * from  device_togwatch where UserCode=#{userCode} limit 1 ")
    DeviceTogWatch getByUserCode(@Param("userCode") String userCode);

    @Select("select  *  from  device_togwatch where Imei=#{imei} limit 1  ")
    DeviceTogWatch getByImei(@Param("imei") String imei);


    @Delete("delete  from device_togwatch  where UserCode=#{userCode} ")
    int deleteByUserCode(@Param("userCode") String userCode);


    @Select("<script>" +
            "select dt.*,u.Name as userName,u.Phone as phone from device_togwatch dt left join user u on dt.UserCode = u.UserCode " +
            "left join user_device ud on ud.UserCode = dt.UserCode " +
            "where dt.Imei is not null " +
            "and ud.ToGWatch = 1 " +
            "<if test ='phone !=null and phone!=\"\" '>" +
            " and u.Phone like concat('%',#{phone},'%') " +
            "</if>" +
            "<if test ='imei !=null and imei!=\"\" '>" +
            " and dt.Imei =#{imei} " +
            "</if>" +
            "</script> ")
    List<DeviceTogWatchDTO> getPage(@Param("phone") String phone,@Param("imei") String imei);






}
